package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.DeviceBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class GroupEnrollmentAdapter  extends AppAdapter<DeviceBean> {
    private boolean canBeEmpty = false;
    private boolean isSingleSelect = false;
    private Map<String, DeviceBean> mSelectedMap = new LinkedHashMap<>();

    public GroupEnrollmentAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public GroupEnrollmentAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new GroupEnrollmentAdapter.ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private TextView roomName;
        private CheckBox checkbox;

        private ViewHolder() {
            super(R.layout.select_room_item);
            roomName = findViewById(R.id.room_name);
            checkbox = findViewById(R.id.checkbox);
        }

        @Override
        public void onBindView(int position) {
            DeviceBean deviceBean = getItem(position);
            checkbox.setTag(getItem(position));
            checkbox.setOnClickListener(mCheckListener);
            roomName.setText(deviceBean.getName());
            checkbox.setText("");
            if (mSelectedMap.get(deviceBean.getDeviceSerialNo())==null){
                checkbox.setChecked(false);
            }else {
                checkbox.setChecked(true);
            }
        }
    }

    private View.OnClickListener mCheckListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DeviceBean deviceBean = (DeviceBean) v.getTag();
            if (isSingleSelect){//单选
                if (((CheckBox)v).isChecked()){
                    mSelectedMap.clear();
                    mSelectedMap.put(deviceBean.getDeviceSerialNo(), deviceBean);
                    onSelectChanged(mSelectedMap);
                }else {
                    ((CheckBox)v).setChecked(true);
                }
            }else {//多选
                if (((CheckBox)v).isChecked()){
                    mSelectedMap.put(deviceBean.getDeviceSerialNo(), deviceBean);
                    onSelectChanged(mSelectedMap);
                }else {
                    mSelectedMap.remove(deviceBean.getDeviceSerialNo());
                    if (!canBeEmpty && mSelectedMap.isEmpty()){
                        mSelectedMap.put(deviceBean.getDeviceSerialNo(), deviceBean);
                    }else {
                        onSelectChanged(mSelectedMap);
                    }
                }
            }
        }
    };

    public ArrayList<DeviceBean> getSelectedData() {
        ArrayList<DeviceBean> checkList = new ArrayList<>();
        for (String key : mSelectedMap.keySet()) {
            checkList.add(mSelectedMap.get(key));
        }
        return checkList;
    }

    public void setSelectedMap(List<DeviceBean> mSelectedMap) {
        if (mSelectedMap != null) {
            for (DeviceBean data : mSelectedMap) {
                this.mSelectedMap.put(data.getRoomSerialNo(), data);
            }
        }
    }

    public void setSelected(Map<String, DeviceBean> selected) {
        this.mSelectedMap = selected;
    }

    public void clearSelected() {
        mSelectedMap.clear();
    }

    public void setCanBeEmpty(boolean value) {
        canBeEmpty = value;
    }

    public void onSelectChanged(Map<String, DeviceBean> selected) {
    }

    public boolean isCanBeEmpty() {
        return canBeEmpty;
    }

    public boolean isSingleSelect() {
        return isSingleSelect;
    }

    public void setSingleSelect(boolean singleSelect) {
        isSingleSelect = singleSelect;
    }
}
